package com.heima.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.PageResult;
import com.heima.pojo.Order;
import com.heima.pojo.vo.OrderCollectVo;
import com.heima.service.IOrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Xxx
 */
@RestController
@RequestMapping("/order-service/report")
public class ReportController {
    @Autowired
    private IOrderService orderService;

    @GetMapping("/orderCount")
    @ApiOperation("获取一定时间范围之内的订单总数")
    public String orderCount(@ApiParam(value = "开始时间", required = true) String start,
                             @ApiParam(value = "结束时间", required = true) String end,
                             @ApiParam(value = "合作商Id", required = false) Integer partnerId,
                             @ApiParam(value = "售货机编号", required = false) String innerCode) {
        LocalDateTime startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd HH:mm:ss");
        LocalDateTime endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd HH:mm:ss");
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(Order::getCreateTime, startTime, endTime).eq(Order::getStatus, 2)
                .eq(partnerId != null, Order::getOwnerId, partnerId)
                .eq(innerCode != null, Order::getInnerCode, innerCode);
        return orderService.count(wrapper) + "";
    }

    @GetMapping("/orderAmount")
    @ApiOperation("获取一定时间范围之内的收入")
    public String orderAmount(@ApiParam(value = "开始时间", required = true) String start,
                              @ApiParam(value = "结束时间", required = true) String end,
                              @ApiParam(value = "合作商Id", required = false) Integer partnerId,
                              @ApiParam(value = "售货机编号", required = false) String innerCode) {
        LocalDateTime startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd HH:mm:ss");
        LocalDateTime endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd HH:mm:ss");
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(Order::getCreateTime, startTime, endTime).select(Order::getPrice)
                .eq(partnerId != null, Order::getOwnerId, partnerId)
                .eq(innerCode != null, Order::getInnerCode, innerCode)
                .eq(Order::getPayStatus, 1);
        List<Order> list = orderService.list(wrapper);
        List<Integer> collect = list.stream().map(item -> item.getPrice()).collect(Collectors.toList());
        return collect.stream().mapToInt(Integer::intValue).sum() + "";
    }

    @GetMapping("/totalBill")
    @ApiOperation("获取一定时间范围之内的分成总数")
    public String totalBill(@ApiParam(value = "开始时间", required = true) String start,
                            @ApiParam(value = "结束时间", required = true) String end,
                            @ApiParam(value = "合作商Id", required = false) Integer partnerId) {
        LocalDateTime startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd HH:mm:ss");
        LocalDateTime endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd HH:mm:ss");
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(Order::getCreateTime, startTime, endTime)
                .eq(partnerId != null, Order::getOwnerId, partnerId)
                .select(Order::getBill);
        List<Order> list = orderService.list(wrapper);
        List<Integer> collect = list.stream().map(item -> item.getBill()).collect(Collectors.toList());
        return collect.stream().mapToInt(Integer::intValue).sum() + "";
    }

    @GetMapping("/partnerCollect")
    @ApiOperation("获取一定日期范围之内的合作商分成汇总数据")
    public PageResult<OrderCollectVo> partnerCollect(PageResult<OrderCollectVo> pageResult, @ApiParam(value = "开始时间", required = true) String start,
                                                     @ApiParam(value = "结束时间", required = true) String end,
                                                     @ApiParam(value = "合作商名称", required = false) String partnerName) {
        return orderService.partnerCollect(pageResult, start, end, partnerName);
    }

    @GetMapping("/regionCollect/{start}/{end}")
    @ApiOperation("根据地区汇总销售额数据(销售量分布)")
    public Map<String, Object> regionCollect(@PathVariable String start, @PathVariable String end) {
        return orderService.regionCollect(start, end);
    }

    @GetMapping("/skuTop/{topValue}/{start}/{end}")
    @ApiOperation("获取一定时间范围之内的销量topN")
    public List<Map<String, Object>> skuTop(@PathVariable Integer topValue, @PathVariable String start, @PathVariable String end) {
        LocalDateTime startTime = LocalDateTimeUtil.parse(start, "yyyy-MM-dd");
        LocalDateTime endTime = LocalDateTimeUtil.parse(end, "yyyy-MM-dd");
        return orderService.skuTop(topValue, startTime, endTime);
    }

    @GetMapping("/amountCollect/{collectType}/{start}/{end}")
    @ApiOperation("获取销售额统计")
    public Map<String, Object> amountCollect(@PathVariable Integer collectType, @PathVariable String start, @PathVariable String end) {
        return orderService.amountCollect(collectType, start, end);
    }
}
